package com.leviassaf.androidbasics;

import java.nio.channels.AlreadyConnectedException;
import java.security.acl.Group;

import android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout.Alignment;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SqlView extends Activity implements OnLongClickListener,
		android.content.DialogInterface.OnClickListener {

	ScaleMeasure info;
	TableRow tableRowSelected;
	int intSelectedId;
	Person person;
	String strSortColName;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.sqlview);
		refresheDisplay();
	}

	private void refresheDisplay() {
		// TODO Auto-generated method stub
		info = new ScaleMeasure(this);
		info.open();
		TableRow[] arrTableRowData = info.getData("person_id", false);
		info.close();

		TableLayout tableLayoutMain = (TableLayout) findViewById(R.id.main_table);
		tableLayoutMain.removeAllViews();
		TableRow tableRowHeader = new TableRow(this);
		tableRowHeader.setId(10);
		tableRowHeader.setBackgroundColor(Color.GRAY);
		tableRowHeader.setLayoutParams(getNewLayoutParams());
		tableRowHeader.setOnLongClickListener(this);

		addHeaderColumn(tableRowHeader, "Id", 20);
		addHeaderColumn(tableRowHeader, "Name", 22);
		addHeaderColumn(tableRowHeader, "Hotness", 23);
		tableLayoutMain.addView(tableRowHeader, getNewLayoutParams());
		for (int i = 0; i < arrTableRowData.length; i++) {
			arrTableRowData[i].setOnLongClickListener(this);
			registerForContextMenu(arrTableRowData[i]);
			tableLayoutMain.addView(arrTableRowData[i], getNewLayoutParams());
		}
		tableLayoutMain.setColumnShrinkable(0, true);
		tableLayoutMain.setColumnStretchable(1, true);
		tableLayoutMain.setColumnStretchable(2, true);
	}

	private TableLayout.LayoutParams getNewLayoutParams() {
		return new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
	}

	private void addHeaderColumn(TableRow TableRowHeader, String ColumnName,
			int Id) {
		TextView textViewName = new TextView(this);
		textViewName.setBackgroundResource(R.drawable.cell_shape);
		textViewName.setId(Id);
		textViewName.setText(ColumnName.toUpperCase());
		textViewName.setTypeface(null, Typeface.BOLD);
		textViewName.setGravity(Gravity.CENTER);
		textViewName.setTextColor(Color.BLUE);
		textViewName.setPadding(5, 5, 5, 5);
		TableRowHeader.addView(textViewName);
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		tableRowSelected = (TableRow) v;
		
		if (tableRowSelected.getId() == 10){
			TextView txtvwClickedHeaderCell = (TextView)tableRowSelected.getChildAt(2);
			strSortColName = txtvwClickedHeaderCell.getText().toString();
			refresheDisplay();
		}
		else{
			
			TextView textViewId = (TextView) tableRowSelected.getChildAt(0);
			intSelectedId = Integer.parseInt(textViewId.getText().toString());
	
			person = new Person();
			person.setId(intSelectedId);
			info.open();
			person.setName(info.getNameById(intSelectedId));
			person.setHotness(info.getHotnessById(intSelectedId));
			info.close();
	
			v.showContextMenu();
		}
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Context Menu");
		menu.add(0, v.getId(), 0, "Delete");
		menu.add(0, v.getId(), 0, "Update");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getTitle().toString() == "Delete") {
			confirmDeleteRecord();
		}
		else if (item.getTitle().toString() == "Update") {
			Intent intent = new Intent("com.leviassaf.speaktranslation.SQLLITE");
			intent.putExtra("key", person);
			startActivity(intent);
		}

		return super.onContextItemSelected(item);
	}

	@Override
	public void onClick(DialogInterface dialog, int whichButton) {
		// TODO Auto-generated method stub
		switch (whichButton){
		case DialogInterface.BUTTON_POSITIVE:
			info.open();
			info.delete(intSelectedId);
			info.close();

			refresheDisplay();
			break;
		case DialogInterface.BUTTON_NEGATIVE:
			dialog.cancel();
			break;
		}
	}

	public void confirmDeleteRecord() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("Confirm Delete...");
		alertDialogBuilder.setMessage("Are you sure you want to delete the selected record?");
		alertDialogBuilder.setIcon(R.drawable.critical);
		alertDialogBuilder.setPositiveButton("YES", this);
		alertDialogBuilder.setNegativeButton("NO", this);
		alertDialogBuilder.show();
	}
}
